# _*_ coding: utf-8 _*_
from odoo import models, fields, api


class ProductProductionAttribute(models.Model):
    _name = "product.production.attribute"
    _order = 'sequence, name'
    _description = u"加工项"

    name = fields.Char(u'名称', required=True)
    value_ids = fields.One2many('product.production.attribute.value', 'attribute_id', 'Values', copy=True)
    sequence = fields.Integer(u'序号', help="Determine the display order")
    # attribute_line_ids = fields.One2many('product.attribute.line', 'attribute_id', 'Lines')
    # create_variant = fields.Boolean(default=True,
    #                                 help="Check this if you want to create multiple variants for this attribute.")


class ProductProductionAttributeValue(models.Model):
    _name = "product.production.attribute.value"
    _order = 'attribute_id,sequence'
    _description = u"加工项说明"

    name = fields.Char(u'名称', required=True)
    sequence = fields.Integer(u'序号', help="Determine the display order")
    attribute_id = fields.Many2one('product.production.attribute', u'加工项', ondelete='cascade', required=True)

    _sql_constraints = [
        ('value_company_uniq', 'unique (name,attribute_id)', u'属性值已经存在！')
    ]


class ProductProductionAttributeLine(models.Model):
    _name = "product.production.attribute.line"

    product_template_id = fields.Many2one("product.template", ondelete="cascade")
    production_id = fields.Many2one("mrp.production", ondelete="cascade", string=u"制造订单")
    product_id = fields.Many2one("product.product", ondelete="cascade")
    attribute_id = fields.Many2one('product.production.attribute', u'加工项', ondelete='cascade', required=True)
    value_ids = fields.Many2many("product.production.attribute.value", relation="prod_line_attr_value_rel",
                                 string=u"加工项说明")

    # _sql_constraints = [
    #     ("product_template_production_attribute_uniq", "unique (product_template_id,attribute_id)", u"需求属性不能重复！"),
    #     ("product_production_attribute_uniq", "unique (product_id,attribute_id)", u"需求属性不能重复！")
    # ]
